//
//  ViewController.m
//  PlayINDemo
//
//  Created by playin on 2019/5/7.
//  Copyright © 2019 playin. All rights reserved.
//

#import "ViewController.h"
#import "PlayIn.h"

@interface ViewController ()<PlayInDelegate>

@property (nonatomic, strong) UIButton *checkButton;
@property (nonatomic, strong) UIButton *playNowButton;
@property (nonatomic, strong) UIButton *closeButton;
@property (nonatomic, strong) PlayIn *playIn;
@property (nonatomic, assign) BOOL isAvailable;
@property (nonatomic, strong) UIView *tmpView;
@property (nonatomic, strong) NSArray *gameArr;
@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, copy) NSString *sdkKey;

@end

@implementation ViewController {
    dispatch_source_t _timeOutTimer;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    

    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBounds.size;
    
    self.gameArr = @[@"6tcjFifA",@"jlp6aQTN",@"2VTT1Q6M",@"MQdPguUV",@"hSXyxiRK",@"NYN1Bort",
                     @"8BG62isZ",@"zoTiJvc2",@"Lcnt1kiA",@"tSKD2fYg",@"Bb4UqPT2",
                     @"AnTGEnYX",@"Ko98TZ01",@"d1EvDDYx",@"CJLJ1w2Z",@"OMKmpKvK"];
    self.titleArr = @[@"多点测试",@"Sniper 3D",@"Score Hero",@"Shark",@"Cookies",@"Color 3D",
                      @"拥挤城市",@"Dune!",@"辐射 避难所",@"Helix Crush",@"Battle Disc",
                      @"Cham pro", @"Escape",@"Likee",@"崩坏3",@"全民弹弹弹"];
    NSInteger cols = 3;
    CGFloat btnWidth = (screenSize.width - 80) / cols;
    CGFloat btnHeight = 50;
    CGFloat margin = 20;
    CGFloat maxY = margin;
    for (int i = 0 ; i < self.gameArr.count; i++) {
       
        CGFloat colMargin = 20;
        NSInteger col = i % cols;
        NSInteger row = i / cols;
        
        CGFloat btnX = col * (btnWidth + colMargin) + colMargin;
        CGFloat btnY = row * (btnHeight + colMargin) + colMargin;
    
        UIButton *checkButton = [[UIButton alloc] init];
        checkButton.frame = CGRectMake(btnX, btnY, btnWidth, btnHeight);
        checkButton.backgroundColor = [UIColor colorWithRed:116/255.0 green:179/255.0 blue:240/255.0 alpha:1];
        checkButton.layer.masksToBounds = YES;
        checkButton.layer.cornerRadius = 8.0f;
        checkButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [checkButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        NSString *titleString = [NSString stringWithFormat:@"%@",self.titleArr[i]];
        [checkButton setTitle:titleString forState:UIControlStateNormal];
        [checkButton addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        checkButton.tag = 100 + i;
        [self.view addSubview:checkButton];
        
        maxY = CGRectGetMaxY(checkButton.frame);
    }
    
    CGRect btn4Rect = CGRectMake(margin, maxY + margin, screenSize.width - margin * 2, btnHeight);
    self.playNowButton = [[UIButton alloc] initWithFrame:btn4Rect];
    self.playNowButton.backgroundColor = [UIColor colorWithRed:255/255.0 green:160/255.0 blue:20/255.0 alpha:1];
    self.playNowButton.layer.masksToBounds = YES;
    self.playNowButton.layer.cornerRadius = 8.0f;
    self.playNowButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.playNowButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.playNowButton setTitle:@"Play Now" forState:UIControlStateNormal];
    //[self.playNowButton addTarget:self action:@selector(playNowButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    self.playNowButton.hidden = YES;
    [self.view addSubview:self.playNowButton];
    
    self.sdkKey = @"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoyNSwidHlwZSI6InNka19rZXkiLCJpYXQiOjE1NTQ4ODg3OTh9.Fphxp0Qgn1A8ZosIhmbnZH78KTWpIYNU_wGKyVB7jBI";
    
//    self.sdkKey = @"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoyMjksInR5cGUiOiJzZGtfa2V5IiwiaWF0IjoxNTY4NzExMTE5fQ.O7yeWVR8_hnaVvvF_Q7piGudOyEgjGcMU9gwgB1SOeA";
    //@"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoyNDMsInR5cGUiOiJzZGtfa2V5IiwiaWF0IjoxNTY4ODgwMjI2fQ.MuQvn8W7CXtvagreD2TyPGFUwCUr2Rp2mfr6g5VButU"; 安卓
}

- (BOOL)prefersStatusBarHidden {

    return YES;
}

- (void)checkButtonTapped:(UIButton *)sender {
    
    self.playIn = [PlayIn sharedInstance];
    self.playIn.delegate = self;
    //__weak typeof(self) weakself = self;
    //o0GPB0RW2GBJwt-9wEKA0Jkv3xC0
    //o0GPB0WhdVWpkANNfEFdYXIFPkpU //o0GPB0RW2GBJwt-9wEKA0Jkv3xC0
    [self.playIn startPlayWithUserId:@"108762427249783" userType:@"2"];
//    [self.playIn playWithUserId:@"o0GPB0WhdVWpkANNfEFdYXIFPkpU" userType:@"1" completionHandler:^(NSDictionary *result) {
//        PIError err = [[result valueForKey:@"code"] integerValue];
//        id info = [result valueForKey:@"info"];
//        if (err == PIErrorNone && [info isKindOfClass:[NSDictionary class]]) {
//            weakself.playNowButton.hidden = YES;
//            [weakself.view addSubview:self.playIn.playInView];
//        } else {
//            NSLog(@"[ViewController] playWithOriginPoint error %@", info);
//        }
//    }];
}

// 4. destroy playin
- (void)destroyPlayIn {
    
    [self.playIn.playInView removeFromSuperview];
    self.playIn.playInView = nil;
}

- (void)closeAction {
    
    [self.playIn stopPlay];
    [self destroyPlayIn];
}

#pragma mark - PlayIn Delegate

- (void)onPlayInStart:(int)duration {
   
    [UIView transitionWithView:self.view duration:2 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
    } completion:^(BOOL finished) {
    }];
    self.playNowButton.hidden = YES;
    [self.view addSubview:self.playIn.playInView];
    
    self.closeButton.frame = CGRectMake(self.view.frame.size.width - 120, 60, 120, 30);
    [self.playIn.playInView addSubview:self.closeButton];
}

- (void)onPlayInError:(NSString *)error {
    
    NSLog(@"[ViewController] playin error: %@", error);
    [self destroyPlayIn];
}

- (void)onPlayInStop {
    
    [self destroyPlayIn];
}

- (UIButton *)closeButton {
    if (!_closeButton) {
        UIButton *button = [[UIButton alloc] init];
        button.userInteractionEnabled = YES;
        button.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        button.titleLabel.textColor = [UIColor whiteColor];
        button.contentMode = UIViewContentModeScaleAspectFit;
        button.titleLabel.adjustsFontSizeToFitWidth = YES;
        [button setTitle:@"Exit Kaleido" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
        _closeButton = button;
    }
    return _closeButton;
}

- (void)dealloc {
    
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
